package com.fh.controller.system.menu;


import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.entity.system.Menu;
import com.fh.service.system.menu.MenuManager;

import net.sf.json.JSONArray;

@Controller
@RequestMapping(value = "/v1/menus/")
public class MenuControllers extends  BaseController {
	
	@Resource(name="menuService")
	private MenuManager menuService;
	
	@RequestMapping(value = "asyloadmenu")
	public ModelAndView asyLoadMenu( HttpServletRequest request,HttpServletResponse response) {
		ModelAndView view = this.getModelAndView();
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
		String parentId = request.getParameter("parentId");
		view.addObject("basePath", basePath);
		try {
			menuService.listSubMenuByParentId(parentId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		view.setViewName("/test/test");
		return view;
	}
	
@RequestMapping( value = "sync")
public void sync( HttpServletRequest request,HttpServletResponse response){
	 String id = request.getParameter("id");  
     String name = request.getParameter("name");  
     String level = request.getParameter("level");  
     String otherParam = request.getParameter("otherParam");  
     System.out.println(id + "|" + name + "|" + level + "|" + otherParam);  
	 String parentId = request.getParameter("parentId");
		if (id != null) {
			parentId = id;
		}
	try {
		List<Menu>  list=menuService.listSubMenuByParentId(parentId);
		List<Map<String, Object>> map =new ArrayList<>();
		Map<String,Object> maps = null;
		for (int i = 0; i < list.size(); i++) {
			maps = new HashMap<>();
			/*if (list.get(i).getMENU_URL().equals("#")) {
				maps.put("isParent", true);
			}else{
				maps.put("isParent", false);
			}
			maps.put("name", list.get(i).getMENU_NAME());
			maps.put("id", list.get(i).getMENU_ID());
			maps.put("pid", list.get(i).getPARENT_ID());*/
			/*maps.put("url", list.get(i).getMENU_URL());*/
			map.add(maps);
		}
		JSONArray json =JSONArray.fromObject(list);
		System.out.println(json);
		PrintWriter out=null;
		response.setCharacterEncoding("utf-8");
		out =response.getWriter();
		out.print(json);
	} catch (Exception e) {
		e.printStackTrace();
	}
}

}
